﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class Shop_EditController : UserController
    {
        public IShopService _shop { get; set; }
        [Authorize]
        [HttpGet]
        public IActionResult Get(string shopname,string img_url,string mobile,string area,string address,string yewu,float longitude, float latitude)
        {
            //检查用户是否登录
            Users userModel = GetUserInfo();
            if (userModel == null)
            {
                return Ok(new { status = 0, msg = "用户尚未登录或已超时！" });
            }
            var shop = _shop.GetFirstByUserId(userModel.id);
            if (!string.IsNullOrEmpty(shopname))
                shop.shopname = shopname;
            if (!string.IsNullOrEmpty(img_url))
                shop.img_url = img_url;
            if (!string.IsNullOrEmpty(mobile))
                shop.mobile = mobile;
            if (!string.IsNullOrEmpty(area))
                shop.area = area;
            if (!string.IsNullOrEmpty(address))
                shop.address = address;
            if (!string.IsNullOrEmpty(yewu))
                shop.yewu = yewu;
            if (longitude > 0 && latitude > 0)
            {
                shop.longitude = longitude;
                shop.latitude = latitude;
            }
            _shop.UpdateField(shop.id, "shopname='" + shop.shopname + "',img_url='" + shop.img_url + "',mobile='" + shop.mobile + "',area='" + shop.area + "',address='" + shop.address + "',yewu='" + shop.yewu + "',longitude=" + shop.longitude + ",latitude=" + shop.latitude);
            var sys = Common.XMLConfig.Sysconfig.loadConfig(_env.ContentRootPath);
            shop.img_url = sys.weburl + shop.img_url;
            for (int i = 0; i < shop.albums.Count; i++)
            {
                shop.albums[i].original_path = sys.weburl + shop.albums[i].original_path;
                shop.albums[i].thumb_path = sys.weburl + shop.albums[i].thumb_path;
            }
            return Ok(new { status = 1, data = shop });
        }
    }
}